package com.ctrip.payment.test.ngtest.mock;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import static org.mockito.Mockito.when;

public abstract class BaseMock {

    @Mock
    protected EntityManager entityManager;
    @Mock
    protected CriteriaBuilder criteriaBuilder;
    @Mock
    protected CriteriaQuery criteriaQuery;
    @Mock
    protected Root<Class> root;
    @Mock
    protected TypedQuery typedQuery;

    @BeforeMethod
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    protected void createMockTypeQuery(Class clazz) {
        when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
        when(criteriaBuilder.createQuery(clazz)).thenReturn(criteriaQuery);
        when(criteriaQuery.from(clazz)).thenReturn(root);
        when(entityManager.createQuery(criteriaQuery)).thenReturn(typedQuery);
    }

}
